package com.ruoyi.wq.service.impl;

import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqFriendApply;
import com.ruoyi.wq.domain.vo.WqFriendApplyVO;
import com.ruoyi.wq.mapper.WqFriendApplyMapper;
import com.ruoyi.wq.service.IWqFriendApplyService;

/**
 * 好友申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-02
 */
@Service
public class WqFriendApplyServiceImpl extends ServiceImpl<WqFriendApplyMapper, WqFriendApply> implements IWqFriendApplyService {
    
	@Autowired
    private WqFriendApplyMapper wqFriendApplyMapper;
	
	@Autowired
    private ISysUserService sysUserService;
	
    /**
     * 查询好友申请
     * 
     * @param id 好友申请主键
     * @return 好友申请
     */
    @Override
    public WqFriendApply selectWqFriendApplyById(String id)
    {
        return wqFriendApplyMapper.selectWqFriendApplyById(id);
    }

    /**
     * 查询好友申请列表
     * 
     * @param wqFriendApply 好友申请
     * @return 好友申请
     */
    @Override
    public List<WqFriendApply> selectWqFriendApplyList(WqFriendApply wqFriendApply)
    {
        return wqFriendApplyMapper.selectWqFriendApplyList(wqFriendApply);
    }

    /**
     * 新增好友申请
     * 
     * @param wqFriendApply 好友申请
     * @return 结果
     */
    @Override
    public WqFriendApply insertWqFriendApply(WqFriendApply wqFriendApply){
    	Long applyUserId = SecurityUtils.getUserId();
    	if(applyUserId.equals(wqFriendApply.getUserId())){
    		throw new ServiceException("不能添加自己");
    	}
    	String message = wqFriendApply.getMessage();
    	SysUser applyUser = sysUserService.getById(applyUserId);
    	if(StringUtils.isBlank(applyUser.getWechat())){
    		throw new ServiceException("请到个人主页完善微信号");
    	}
    	SysUser user = sysUserService.getById(wqFriendApply.getUserId());
    	
    	QueryWrapper<WqFriendApply> queryWrapper = new QueryWrapper<>();
    	WqFriendApply entiy = new WqFriendApply();
    	entiy.setApplyUserId(applyUserId);
    	entiy.setUserId(wqFriendApply.getUserId());
    	queryWrapper.setEntity(entiy);
    	wqFriendApply = wqFriendApplyMapper.selectOne(queryWrapper);
    	if(wqFriendApply == null){
    		wqFriendApply = new WqFriendApply();
        	wqFriendApply.setApplyUserId(applyUserId);
        	wqFriendApply.setAge(applyUser.getAge());
        	wqFriendApply.setWechat(applyUser.getWechat());
        	wqFriendApply.setApplyAvatar(applyUser.getAvatar());
        	wqFriendApply.setApplyNickName(applyUser.getNickName());
        	wqFriendApply.setPhone(applyUser.getPhonenumber());
        	wqFriendApply.setSex(applyUser.getSex());
        	wqFriendApply.setMessage("你好！");
        	
        	wqFriendApply.setUserId(user.getUserId());
        	wqFriendApply.setAvatar(user.getAvatar());
        	wqFriendApply.setNickName(user.getNickName());
            wqFriendApply.setCreateTime(DateUtils.getNowDate());
            wqFriendApplyMapper.insert(wqFriendApply);
    	}else{
    		if("1".equals(wqFriendApply.getStatus())){
    			throw new ServiceException("请添加微信交流");
    		}
    		
    		if("2".equals(wqFriendApply.getStatus())){
    			throw new ServiceException("对方已拒绝你的好友申请");
    		}
    		
    		wqFriendApply.setMessage(message);
    		wqFriendApply.setCreateTime(DateUtils.getNowDate());
    		wqFriendApplyMapper.updateById(wqFriendApply);
    	}
    	
        return wqFriendApply;
    }

    /**
     * 修改好友申请
     * 
     * @param wqFriendApply 好友申请
     * @return 结果
     */
    @Override
    public int updateWqFriendApply(WqFriendApply wqFriendApply)
    {
        wqFriendApply.setUpdateTime(DateUtils.getNowDate());
        return wqFriendApplyMapper.updateWqFriendApply(wqFriendApply);
    }

    /**
     * 批量删除好友申请
     * 
     * @param ids 需要删除的好友申请主键
     * @return 结果
     */
    @Override
    public int deleteWqFriendApplyByIds(String[] ids)
    {
        return wqFriendApplyMapper.deleteWqFriendApplyByIds(ids);
    }

    /**
     * 删除好友申请信息
     * 
     * @param id 好友申请主键
     * @return 结果
     */
    @Override
    public int deleteWqFriendApplyById(String id)
    {
        return wqFriendApplyMapper.deleteWqFriendApplyById(id);
    }

	@Override
	public TableDataInfo getMyList(WqFriendApply wqFriendApply) {
		QueryWrapper<WqFriendApply> qw = Condition.getQueryWrapper(wqFriendApply);
		wqFriendApply.setApplyUserId(SecurityUtils.getUserId());
        qw.setEntity(wqFriendApply);
        qw.orderByDesc("create_time");
        IPage<WqFriendApply> page = baseMapper.selectPage(Condition.getPage(wqFriendApply), qw);
        return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public TableDataInfo getMyApply(WqFriendApply wqFriendApply) {
		QueryWrapper<WqFriendApply> qw = Condition.getQueryWrapper(wqFriendApply);
		wqFriendApply.setUserId(SecurityUtils.getUserId());
        qw.setEntity(wqFriendApply);
        qw.orderByDesc("apply_num");
        qw.orderByDesc("create_time");
        IPage<WqFriendApply> page = baseMapper.selectPage(Condition.getPage(wqFriendApply), qw);
        return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public WqFriendApplyVO getDetail(String id) {
		WqFriendApply wqFriendApply = baseMapper.selectById(id);
		
		QueryWrapper<WqFriendApply> qw = new QueryWrapper<>();
		WqFriendApply entity = new WqFriendApply();
		entity.setUserId(wqFriendApply.getUserId());
		entity.setStatus("0");
        qw.setEntity(entity);
        qw.orderByDesc("apply_num");
        qw.orderByDesc("create_time");
		List<WqFriendApply> applyList = baseMapper.selectList(qw);
		Integer rankNum = 1;
		for(int i=0;i<applyList.size();i++){
			WqFriendApply apply = applyList.get(i);
			if(apply.getId().equals(id)){
				rankNum = i+1;
			}
		}
		wqFriendApply.setRankNum(rankNum);
		baseMapper.updateById(wqFriendApply);
		
		WqFriendApplyVO applyVO = new WqFriendApplyVO();
		BeanUtils.copyProperties(wqFriendApply, applyVO);
		applyVO.setApplyList(applyList);
		return applyVO;
	}

	@Override
	public WqFriendApply getRoom(String id) {
		WqFriendApply wqFriendApply = baseMapper.selectById(id);
		SysUser user = sysUserService.getById(wqFriendApply.getUserId());
		if("0".equals(wqFriendApply.getUnRead())){
			// 未读改成已读
			wqFriendApply.setUnRead("1");
			baseMapper.updateById(wqFriendApply);
			
			// 增加查看消息次数
			Integer readNum = user.getReadNum();
			user.setReadNum(readNum+1);
			sysUserService.updateById(user);
		}
		wqFriendApply.setContact(user.getWechat());
		return wqFriendApply;
	}

	@Override
	public List<WqFriendApply> getUnRead() {
		QueryWrapper<WqFriendApply> qw = new QueryWrapper<>();
		WqFriendApply entity = new WqFriendApply();
		entity.setUserId(SecurityUtils.getUserId());
		entity.setUnRead("0");
        qw.setEntity(entity);
        qw.orderByDesc("apply_num");
        qw.orderByDesc("create_time");
		return baseMapper.selectList(qw);
	}

}
